using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace BatchPlot
{
    public class Publisher
    {
        public void RunAUTOCAD(string pathToACAD, string pathToSciptFile, bool wait)
        {
            System.Diagnostics.ProcessStartInfo psi =
                 new System.Diagnostics.ProcessStartInfo(pathToACAD, @"/nologo /b " + "\"" + pathToSciptFile + "\"");
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            psi.CreateNoWindow = true;
            psi.UseShellExecute = true;
            // MessageBox.Show(psi.Arguments);
            System.Diagnostics.Process listFiles;
            listFiles = System.Diagnostics.Process.Start(psi);
            if (wait)
                listFiles.WaitForExit();

        }

        public List<string> GetPageSetupList2007(string batchDir, string pathToACAD, string drawingFile)
        {
            string scriptFile = batchDir + "\\getpagesetup.scr";
            List<string> ret = new List<string>();
            // create a writer and open the file
            try
            {
                TextWriter tw = new StreamWriter(scriptFile);
                tw.WriteLine("NETLOAD");
                tw.WriteLine("\"" + batchDir + "\\BatchPlot2007.dll\"");
                tw.WriteLine("PageSetupList");
                tw.WriteLine("\"" + drawingFile + "\"");
                tw.WriteLine("\"" + batchDir + "\\pagesetuplist.txt\"");
                tw.Close();

                //appli
                RunAUTOCAD(pathToACAD, batchDir + "\\getpagesetup.scr", true);

                TextReader tr = new StreamReader(batchDir + "\\pagesetuplist.txt");
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    ret.Add(line);
                }

                tr.Close();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }

            return ret;

        }

        public List<string> GetPageSetupList2006(string batchDir, string pathToACAD, string drawingFile)
        {
            string scriptFile = batchDir + "\\getpagesetup.scr";
            List<string> ret = new List<string>();
            // create a writer and open the file
            try
            {
                TextWriter tw = new StreamWriter(scriptFile);
                tw.WriteLine("NETLOAD");
                tw.WriteLine("\"" + batchDir + "\\BatchPlot2006.dll\"");
                tw.WriteLine("PageSetupList");
                tw.WriteLine("\"" + drawingFile + "\"");
                tw.WriteLine("\"" + batchDir + "\\pagesetuplist.txt\"");
                tw.Close();

                RunAUTOCAD(pathToACAD, batchDir + "\\getpagesetup.scr", true);

                TextReader tr = new StreamReader(batchDir + "\\pagesetuplist.txt");
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    ret.Add(line);
                }

                tr.Close();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }

            return ret;

        }

        public void Publish2007(string ACADPath, string batchDir, string drawingDirPath, string pageSetupFilePath, string PageSetupName)
        {
            string scriptFile = batchDir + "\\publish.scr";
            // create a writer and open the file
            try
            {
                //TextWriter tw = new StreamWriter(scriptFile);
                TextWriter tw = File.CreateText(scriptFile);
                //tw.WriteLine("+publish");
                //tw.WriteLine("D:\\vudang\\work\\ArchSoft\\dwg\\MechanicalMetric_Aucad.dsd");

                tw.WriteLine("NETLOAD");
                tw.WriteLine("\"" + batchDir + "\\BatchPlot2007.dll\"");
                tw.WriteLine("BatchDWF");
                tw.WriteLine("\"" + drawingDirPath + "\"");
                tw.WriteLine("*.dwg");
                tw.WriteLine("y"); // no subdir
                tw.WriteLine("\"" + drawingDirPath + "\"");
                tw.WriteLine("n"); // no model space
                //tw.WriteLine("y");
                //tw.WriteLine("\"" + pageSetupFilePath + "\"");
                //tw.WriteLine("\"" + PageSetupName + "\"");
                //tw.WriteLine("n");
                tw.WriteLine("y");
                tw.WriteLine("\"" + pageSetupFilePath + "\"");
                tw.WriteLine("\"" + PageSetupName + "\"");
                tw.WriteLine("y");
                tw.WriteLine("n");

                tw.Close();
                RunAUTOCAD(ACADPath, batchDir + "\\publish.scr", true);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }

        }


        public void Publish2006(string ACADPath, string batchDir, string drawingDirPath, string pageSetupFilePath, string PageSetupName)
        {
            string scriptFile = batchDir + "\\publish.scr";
            // create a writer and open the file
            try
            {
                TextWriter tw = new StreamWriter(scriptFile);
                //tw.WriteLine("+publish");
                //tw.WriteLine("D:\\vudang\\work\\ArchSoft\\dwg\\MechanicalMetric_Aucad.dsd");

                tw.WriteLine("NETLOAD");
                tw.WriteLine("\"" + batchDir + "\\BatchPlot2006.dll\"");
                tw.WriteLine("BatchDWF");
                tw.WriteLine("\"" + drawingDirPath + "\"");
                tw.WriteLine("*.dwg");
                tw.WriteLine("y"); // no subdir
                tw.WriteLine("\"" + drawingDirPath + "\"");
                tw.WriteLine("n"); // no model space
                //tw.WriteLine("y");
                //tw.WriteLine("\"" + pageSetupFilePath + "\"");
                //tw.WriteLine("\"" + PageSetupName + "\"");
                //tw.WriteLine("n");
                tw.WriteLine("y");
                tw.WriteLine("\"" + pageSetupFilePath + "\"");
                tw.WriteLine("\"" + PageSetupName + "\"");
                tw.WriteLine("y");
                tw.WriteLine("n");
                tw.WriteLine("+publish");
                tw.WriteLine(drawingDirPath + "\\batchplot.dsd");
                tw.WriteLine("Quit");

                tw.Close();
                RunAUTOCAD(ACADPath, batchDir + "\\publish.scr", true);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }

        }

    }
}
